import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  //数据，属性
  state: {
    username:'Jack',
  },
  //getter
  getters: {
    fullName: state => state.username + " Chan",
  },
  //setter
  mutations: {
    SET_NAME:(state, username)=>{
      state.username = username;
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    }
  },
  //自定义方法调用setter
  actions: {
    updateUserName({commit}, newUserName){
      //加入异步操作代码，发起Ajax请求
      // 调用mutation提交操作
      commit('SET_NAME',newUserName)
    },
    login({ commit }, userInfo) {
      // 项目后期进行ajxa进行登录验证
      const { username, password } = userInfo
      commit('SET_NAME', username)
      commit('SET_TOKEN', 'admin_token')
    }
  },
  modules: {
    //需要模块化的时候使用
  }
})
